package cn.chencq.redis.demo;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import cn.chencq.redis.cluster.RedisClusterFactory;
import cn.chencq.redis.cluster.util.Utils;

/**
 * @desc 公共Redis 操作
 */
public class CommonRedisService
{
	
	private static Properties redisProperties = Utils.getProperties("redis.properties");

	/**
	 * 获取要使用的集群的名称
	 * 
	 * @return Redis集群名称
	 */
	private static String getClusterName()
	{
		return redisProperties.getProperty("redis.cluster.name");
	}

	
	/**
	 * 获取某一个set的元素
	 * @param key
	 */
	public static Set<String> setMembers(String key)
	{
		return RedisClusterFactory.smembers(getClusterName(), key);
	}


	/**
	 * 查询匹配的所有的key
	 * @param pattern
	 * @return
	 */
	public static Set<String> keys(String pattern)
	{
		return RedisClusterFactory.keys(getClusterName(), pattern);
	}
	
	

	/**
	 * 获取Map多个Field值
	 * @param key 
	 * @param fields
	 * @return
	 */
	public static List<String> getMapMulField(String key, String... fields)
	{
		return RedisClusterFactory.hmget(getClusterName(), key, fields);
	}
	
	
	/**
	 * 操作集合set-添加字符到集合
	 * @param key
	 * @param member
	 */
	public static void setAddString(String key, String ...member)
	{
		RedisClusterFactory.sadd(getClusterName(), key, member);
	}
	
	
	/**
	 * 操作集合set-添加指定的Set集合数据到set
	 * @param key 
	 * @param set 字符集合
	 */
	public static void setAddString(String key, Set<String> set)
	{
		String members[] = new String[set.size()];
		set.toArray(members);
		setAddString(key, members);
	}
	
	
	/**
	 * 删除Redis某个Key对应的存储
	 * @param keys
	 */
	public static void del(String ...keys)
	{
		RedisClusterFactory.del(getClusterName(), 
				keys);
	}
	
	
	/**
	 * 删除Redis某个Key对应的存储
	 * @param keys
	 */
	public static void del(Set<String> set)
	{
		String keys[] = new String[set.size()];
		set.toArray(keys);
		RedisClusterFactory.del(getClusterName(), 
				keys);
	}
	
	
	/**
	 * 求差集将结果存储在dstKey中
	 * @param dstkey 存储的key
	 * @param keys 参与求差集的key
	 */
	public static void setDiffStore(String dstkey, String ...keys)
	{
		RedisClusterFactory.sdiffstore(getClusterName(), dstkey, keys);
	}
	
	
	/**
	 * 移除set的元素
	 * @param key set的key
	 * @param members 要移除的元素列表
	 */
	public static void setRemoveMembers(String key, String ...members)
	{
		RedisClusterFactory.srem(getClusterName(), key, members);
	}
	
	/**
	 * 移除set的元素
	 * @param key set的key
	 * @param members 移除元素的列表
	 */
	public static void setRemoveMembers(String key, Set<String> set)
	{
		String members[] = new String[set.size()];
		set.toArray(members);
		setRemoveMembers(key, members);
	}
	
	
	/**
	 * 判断是不是集合的元素
	 * @param key
 	 * @param member
	 */
	public static boolean setIsMember(String key, String member)
	{
		return RedisClusterFactory.sismember(getClusterName(), key, member);
	}
	
	
	/**
	 * 删除Map 一个或若干个Field
	 * 
	 * @param key
	 * @param fields
	 */
	public static void hdel(String key, String ...fields)
	{
		RedisClusterFactory.hdel(getClusterName(), key, fields);
	}
	
	
	/**
	 * 删除Map 一个或若干个Field
	 * 
	 * @param key
	 * @param fieldSet
	 */
	public static void hdel(String key, Set<String> fieldSet)
	{
		if (fieldSet.size() > 0)
		{
			String fields[] = new String[fieldSet.size()];
			
			fieldSet.toArray(fields);
			
			RedisClusterFactory.hdel(getClusterName(), key, fields);
		}
	}
	
	
	/**
	 * 存储哈希
	 * @param key 
	 * @param hash
	 */
	public static void hmset(String key,  Map<String, String> hash)
	{
		try
		{
			RedisClusterFactory.hmset(getClusterName(), key, hash);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	
	public static String hget(String key, String field)
	{
		return RedisClusterFactory.hget(getClusterName(), key, field);
	}
	
	
	public static List<String> hmget(String key, String ...fields)
	{
		return RedisClusterFactory.hmget(getClusterName(), key, fields);
	}
	
	
	public static Map<String,String> hgetAll(String key)
	{
		return RedisClusterFactory.hgetAll(getClusterName(), key);
	}

	
	/**
	 * 通过Key获取value
	 * 
	 * @param key
	 * @return
	 */
	public static String getVByK(String key)
	{
		return RedisClusterFactory.get(getClusterName(), key);
	}
	
	
	/**
	 * 设置key的超时时间
	 * 
	 * @param k
	 *            key值
	 * @param seconds
	 *            超时的秒数
	 * 
	 */
	public static void setKeyTimeOut(String key, int seconds)
	{
		RedisClusterFactory.expire(getClusterName(), key, seconds);
	}
	

	/**
	 * 将key的过期时间设置清除
	 * 
	 * @param key
	 */
	public static void persistKey(String key)
	{
		RedisClusterFactory.persist(getClusterName(), key);
	}
	
	/**
	 * 判断key值是否存在
	 * 
	 * @param key
	 *            key值
	 * @return true/false 存在/不存在
	 */
	public static boolean isKeyExists(String key)
	{
		return RedisClusterFactory.exists(getClusterName(), key);
	}
	
	
	/**
	 * 存储K-V
	 * @param key
	 * @param value
	 */
	public static void storeKV(String key, String value)
	{
		RedisClusterFactory.set(getClusterName(), key, value);
	}
	
	/**
	 * 批处理存储k-v
	 * 
	 * @param kvList
	 *            ["k-1", "v-1", "k-2", "v-2"] 长度须是偶数
	 */
	public static void storeKVBatch(List<String> kvList)
	{
		if (kvList.size() > 0)
		{
			String kvs[] = new String[]{};
			kvs = kvList.toArray(kvs);
			
			RedisClusterFactory.mset(getClusterName(), kvs);
		}
	}
}
